import java.util.Random;


public class fenshu {
    private int numerator, denominator;


    public  fenshu (int numer, int denom)
    {
        if(denom == 0 )
            denom = 1;

        if (denom < 0)
        {
            numer = numer * -1;
            denom = denom * -1;
        }

        numerator = numer;
        denominator = denom;

        reduce();
    }

    public int getNumerator()
    {
        return numerator;
    }

    public int getDenominator()
    {
        return denominator;
    }

    public fenshu reciprocal()
    {
        return new fenshu (denominator, numerator);
    }

    public fenshu add(fenshu op2)
    {
        int commonDenominator = denominator * op2.getDenominator();
        int numerator1 = numerator * op2.getDenominator();
        int numerator2 = op2.getNumerator() * denominator;
        int sum = numerator1 + numerator2;
        System.out.print("("+this.toString()+")" + " + " + "("+op2.toString()+")" + "=");
        return new fenshu (sum, commonDenominator);
    }

    public fenshu subtract(fenshu op2)
    {
        int commonDenominator = denominator * op2.getDenominator();
        int numerator1 = numerator * op2.getDenominator();
        int numerator2 = op2.getNumerator() * denominator;
        int difference = numerator1 - numerator2;
        System.out.print("("+this.toString()+")" + " - " + "("+op2.toString()+")" + "=");
        return new fenshu(difference,commonDenominator);
    }

    public fenshu multiply (fenshu op2)
    {
        int numer = numerator * op2.getNumerator();
        int denom = denominator * op2.getDenominator();
        System.out.print("("+this.toString()+")" + " * " + "("+op2.toString()+")" + "=");
        return new fenshu (numer, denom);
    }

    public fenshu divide (fenshu op2)
    {
        fenshu op1 = op2.reciprocal();
        int numer = numerator * op1.getNumerator();
        int denom = denominator * op1.getDenominator();
        System.out.print("("+this.toString()+")" + " / " + "("+op2.toString()+")" + "=");
        return new fenshu (numer, denom);
    }

    public boolean isLike (fenshu op2)
    {
        return (numerator == op2.getNumerator() &&
                denominator == op2.getDenominator());
    }

    public String toString()
    {
        String result;

        if (numerator == 0)
            result = "0";
        else
        if (denominator == 1)
            result = numerator + "";
        else
            result = numerator + "/" + denominator;

        return result;
    }

    private void reduce()
    {
        if (numerator != 0)
        {
            int common = gcd (Math.abs(numerator), denominator);

            numerator = numerator / common;
            denominator = denominator / common;
        }
    }

    private int gcd (int num1, int num2)
    {
        while (num1 != num2)
            if (num1 > num2)
                num1 = num1 - num2;
            else
                num2 = num2 - num1;

        return num1;
    }

    public static fenshu obj(){
        Random ran = new Random();
        return new fenshu(ran.nextInt(20)-10,ran.nextInt(20)-10);
    }
}
